package tq.com.tqcom.util;

import android.app.Dialog;
import android.content.Context;
import android.widget.TextView;

import tq.com.tqcom.R;

public class TQLoadingHelper {
    static private TQLoadingHelper sLoadingHelper;

    private Dialog mDialog = null;
    private Context mContext;
    private TextView mTextView;

    static public TQLoadingHelper build(Context pContext){
        if(sLoadingHelper != null){
            sLoadingHelper.dismiss();
        }

        sLoadingHelper = new TQLoadingHelper(pContext);
        return sLoadingHelper;
    }

    static public void dimiss(){
        if(sLoadingHelper != null){
            sLoadingHelper.dismiss();
        }
    }

    static public void setText(String txt){
        if(sLoadingHelper != null){
            sLoadingHelper.mTextView.setText(txt);
        }
    }
    static public void setError(String txt){
        if(sLoadingHelper != null){
            sLoadingHelper.setCancelable(true);
            sLoadingHelper.mTextView.setText(txt);
        }
    }
    static public void setHelpCancelable(boolean b){
        if(sLoadingHelper != null){
            sLoadingHelper.setCancelable(b);
        }
    }

    public TQLoadingHelper(Context pContext){
        mContext = pContext;

        mDialog = new Dialog(pContext, R.style.LoaddingDailog);
        mDialog.setContentView(R.layout.loading_layout);

        mTextView =mDialog.findViewById(R.id.text);
        mTextView.setText("");
    }
    public void show(){
        mDialog.show();
    }

    public TQLoadingHelper setCancelable(boolean pCancelable){
        mDialog.setCancelable(pCancelable);
        mDialog.setCanceledOnTouchOutside(pCancelable);
        return this;
    }

    public void dismiss(){
        mDialog.dismiss();
    }

}
